package chapter03;

/**
 * 3.31使用单链表高效实现栈类，不用头节点和尾节点
 */
public class SingleStack<E> {

    private Node<E> head;

    public SingleStack() {
        head = null;
    }

    void push(E e) {
        Node<E> p = new Node<>(e, head);
        head = p;
    }

    E peek() {
        return head.item;
    }

    void pop() {
        head = head.next;
    }

    private class Node<E> {
        E item;
        Node next;

        public Node() {
            this(null);
        }

        public Node(E item) {
            this(item, null);
        }

        public Node(E item, Node next) {
            this.item = item;
            this.next = next;
        }
    }


}
